#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        vector<int> index(n);
        for (int i = 0; i < n; i++) index[i] = i;
        sort(nums1.begin(), nums1.end());
        sort(index.begin(), index.end(), [&](int i, int j) {
            return nums2[i] < nums2[j];
            });
        int left = 0, right2 = n - 1, left2 = 0;
        vector<int> ret(n);
        while (left < n)
        {
            if (nums1[left] > nums2[index[left2]]) ret[index[left2++]] = nums1[left++];
            else ret[index[right2--]] = nums1[left++];
        }
        return ret;
    }
};